package com.soul.dj.ui.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.soul.dj.MainApplication;
import com.soul.dj.R;
import com.soul.dj.model.User;
import com.soul.dj.util.http.json.JsonProcessUtil;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.util.*;

/**
 * Created by soul on 2016/3/3.
 */
public class MineFragment extends BaseFragment {

    private ListView listView;

    private List<Map<String,Object>> items = new ArrayList<Map<String,Object>>();
    private ArrayAdapter<Map<String,Object>> adapter;

    private int page = 1;
    private boolean pull_up_lock;// 锁定上拉
    private View footerView;

    private int lastVisibleItemPosition = 0;// 标记上次滑动位置，初始化默认为0
    private boolean scrollFlag = false;// 标记是否滑动


    private Handler  handler = new Handler();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        activity = getActivity();

        View v = inflater.inflate(R.layout.mine_fragment, container, false);

        String logTime = DateFormatUtils.format(Long.valueOf(MainApplication.getMap(User.LOGTIME).toString()),"yyyy-MM-dd HH:mm:ss");
        ((TextView)v.findViewById(R.id.name)).setText(MainApplication.getMap(User.LOGID).toString());
        ((TextView)v.findViewById(R.id.loginTime)).setText(logTime);

        listView = v.findViewById(R.id.listView);

        adapter = new ArrayAdapter<Map<String,Object>>(activity, android.R.layout.simple_list_item_1, items){

            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {

                View	view = convertView;
                final ViewHolder holder;

                final Map<String,Object> item = items.get(position);
                if(view == null){
                    view = getLayoutInflater().inflate(R.layout.mine_list_item, null);
                    holder = new ViewHolder();
                    holder.time = view.findViewById(R.id.time);
                    holder.cnt = view.findViewById(R.id.cnt);

                    view.setTag(holder);
                }else{
                    holder = (ViewHolder) view.getTag();
                }
                view.setOnClickListener(new View.OnClickListener(){
                    @Override
                    public void onClick(View v) {

                    }
                });

                holder.time.setText(item.get("CHECKDATE").toString());
                holder.cnt.setText(item.get("CHECKNUM").toString());

                return view;
            }
        };

        listView.setAdapter(adapter);

        return v;
    }


    @Override
    public void onResume() {
        super.onResume();
        setProgressEnabled(false);
        Map<String,Object> params = new HashMap<>();
        params.put("checkman", MainApplication.getMap(User.LOGID));
        params.put("datefrom", DateFormatUtils.format(DateUtils.addMonths(new Date(),-1),"yyyy-MM-dd"));
        params.put("dateto",DateFormatUtils.format(new Date(),"yyyy-MM-dd"));

//        sendHttp(Const.REQUEST_URL_GETADMINWORKLIST, MediaType.JSON,params,new JSONCallBack<List>(this,"lodeData",List.class));
    }

    @Override
    public void onAjaxOK(Object result, String tag) {
        super.onAjaxOK(result, tag);
        if(TextUtils.equals("lodeData", tag)){
            List list = (List) result;
            items.clear();
            if (list != null && list.size() > 0) {
                for (Object object : list) {
                    try {
                        String tmp = JsonProcessUtil.beanToJson(object);
                        Map<String,Object> good = JsonProcessUtil.jsonToBean2(tmp , Map.class);
                        items.add(good);
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }

                page++;
            }
            new Thread(){
                public void run(){
                    handler.post(runnableUi);
                }
            }.start();
        }
    }

    Runnable   runnableUi=new  Runnable(){
        @Override
        public void run() {
            //更新界面
            adapter.notifyDataSetChanged();
        }

    };

    private class ViewHolder{

        private TextView time,cnt;
    }
}
